

import tensorflow as tf

#读取csv数据
filename_queue = tf.train.string_input_producer(["./haixue_pre_handle_data_1506092799599.csv", "./haixue_pre_handle_data_1506111443149.csv",
	"./haixue_pre_handle_data_1506130004134.csv"])

reader = tf.TextLineReader()
key, value = reader.read(filename_queue)

# Default values, in case of empty columns. Also specifies the type of the
# decoded result.
record_defaults = [[0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0]]

col1, col2, col3, col4, col5, col6, col7, col8, col9, col10, col11, col12, col13, col14, col15, col16, col17, col18, col19, col20, col21, col22 = tf.decode_csv(value, record_defaults=record_defaults)

#print("col1: %s, col2: %s, col3: %s, col4: %s, col5: %s, col6: %s, col7: %s, col8: %s, col9: %s, col10: %s, col11: %s" % 
#	(col1, col2, col3, col4, col5, col6, col7, col8, col9, col10, col11))


features = [col3, col4, col5, col6, col7, col8, col9, col10, col11, col12, col13, col14, col15, col16, col17, col18, col19, col20]
judgeLabel = [col21, col22]

x = tf.placeholder(tf.float32, [None, 18])
#print(tf.shape(x))
W = tf.Variable(tf.zeros([18, 2]))
#print(tf.shape(W))
b = tf.Variable(tf.zeros([2]))
#print(tf.shape(b))
y = tf.matmul(x, W) + b
#print(tf.shape(y))
# Define loss and optimizer
y_ = tf.placeholder(tf.float32, [None, 2])

#cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))
cross_entropy = tf.reduce_mean(
      tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y))
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)
totalAccuracy = 0

saver = tf.train.Saver()

# Train
with tf.Session() as sess:
	sess.run(tf.global_variables_initializer())
	coord = tf.train.Coordinator()
	threads = tf.train.start_queue_runners(coord=coord)

	batch_xs, batch_ys = [], []
	for i in range(300000):
		example, label = sess.run([features, judgeLabel])
		batch_xs.append(example)
		batch_ys.append(label)
		#print(example)
		#print(label)
		data = [example]
		#print(data)
		label2 = [label]
		#print(label2)
		sess.run(train_step, feed_dict={x: data, y_: label2})
#		correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))
#		accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
#		train_accuracy = accuracy.eval(feed_dict={x: data, y_: label2})
#		totalAccuracy += train_accuracy

		if i % 100 == 0:
#			accuracyPercent = totalAccuracy / 100
#			print('step %d, training accuracy %g' % (i, accuracyPercent))
			correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))
			accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
			train_accuracy = accuracy.eval(feed_dict={x: batch_xs, y_: batch_ys})
			print('step %d, training accuracy %g' % (i, train_accuracy))
			#print('step %d' % (i))
#			totalAccuracy = 0
			batch_xs, batch_ys = [], []
	
	saver.save(sess, "Model/haixue_model.ckpt")
	print("---------------test--------------")
# Test trained model
#	for i in range(10000, 10100):
#		example2, label2 = sess.run([features, judgeLabel])
#		print(example2)
#		print(label2)
#		correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))
#		accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
#		train_accuracy = accuracy.eval(feed_dict={x: [example2], y_: [label2]})
		
#		totalAccuracy += train_accuracy
#		if i % 100 == 0:
#			accuracyPercent = totalAccuracy / 100
#			print('step %d, test accuracy %g' % (i, accuracyPercent))
#			totalAccuracy = 0
		#print(sess.run(accuracy, feed_dict={x: [example2], y_: [label2]}))
		#print("---------------------")




